package MuleBackend;

import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * A client class for use with the server class for network functionality
 * 
 * @author Phil
 */

public class Client implements Runnable {

  private static Socket clientSocket = null;
  private static PrintStream os = null;
  private static DataInputStream is = null;
  static int portNumber = 3985;


  private static BufferedReader inputLine = null;
  private static boolean closed = false;
  
  public static void main(String[] args) throws UnknownHostException {

    String host = InetAddress.getLocalHost().getHostAddress();

    try 
    {
      clientSocket = new Socket("localhost", portNumber);
      System.out.println("Connection successful!");
      inputLine = new BufferedReader(new InputStreamReader(System.in));
      os = new PrintStream(clientSocket.getOutputStream());
      is = new DataInputStream(clientSocket.getInputStream());
      System.out.println("Now using host: " + host + ", portNumber: " + portNumber);
    } 
      catch (UnknownHostException e) 
    {
      System.err.println("Invalid host: " + host);
    } catch (IOException e) 
    {
      System.err.println("IO error with host:  " + host);
    }

    /*
     * If everything has been initialized then we want to write some data to the
     * socket we have opened a connection to on the port portNumber.
     */
    if (clientSocket != null && os != null && is != null) {
      try {

        /* Create a thread to read from the server. */
        new Thread(new Client()).start();
        while (!closed) {
          os.println(inputLine.readLine().trim());
        }
        
        os.close();
        is.close();
        clientSocket.close();
      } catch (IOException e) {
        System.err.println("IOException:  " + e);
      }
    }
  }
  
  public static void runServer() throws UnknownHostException {
	 
	    String host = InetAddress.getLocalHost().getHostAddress();

	    try 
	    {
	      clientSocket = new Socket("localhost", portNumber);
	      System.out.println("Connection successful!");
	      inputLine = new BufferedReader(new InputStreamReader(System.in));
	      os = new PrintStream(clientSocket.getOutputStream());
	      is = new DataInputStream(clientSocket.getInputStream());
	      System.out.println("Now using host: " + host + ", portNumber: " + portNumber);
	    } 
	      catch (UnknownHostException e) 
	    {
	      System.err.println("Invalid host: " + host);
	    } catch (IOException e) 
	    {
	      System.err.println("IO error with host:  " + host);
	    }

	    /*
	     * If everything has been initialized then we want to write some data to the
	     * socket we have opened a connection to on the port portNumber.
	     */
	    if (clientSocket != null && os != null && is != null) {
	      try {

	        /* Create a thread to read from the server. */
	        new Thread(new Client()).start();
	        while (!closed) {
	          os.println(inputLine.readLine().trim());
	        }
	        
	        os.close();
	        is.close();
	        clientSocket.close();
	      } catch (IOException e) {
	        System.err.println("IOException:  " + e);
	      }
	    }
	  
	}
  

  public void run() {
    /*
     * Keep on reading from the socket till we receive "Bye" from the
     * server. Once we received that then we want to break.
     */
    String responseLine;
    try {
      while ((responseLine = is.readLine()) != null) {
        System.out.println(responseLine);
        if (responseLine.indexOf("User: ") != -1)
          break;
      }
      closed = true;
    } catch (IOException e) {
      System.err.println("IOException:  " + e);
    }
  }
}